package com.ggbz.controller;


import com.ggbz.common.utils.JwtUtil;
import com.ggbz.common.utils.Result;
import com.ggbz.entity.EvaluationCategories;
import com.ggbz.entity.Users;
import com.ggbz.entity.dto.EvaluationCategoryRequest;
import com.ggbz.entity.dto.EvaluationCategoryUpdateRequest;
import com.ggbz.service.EvaluationCategoriesService;
import com.ggbz.service.UsersService;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 评分大类表，存储评分的大类信息，例如竞赛、实践等，每个大类包含权重 前端控制器
 * </p>
 *
 * @author ggbz
 * @since 2025-03-08
 */
@RestController
@RequestMapping("/evaluation-categories")
public class EvaluationCategoriesController {
    @Autowired
    private EvaluationCategoriesService categoriesService;

    @Autowired
    private UsersService usersService;
    @GetMapping
    public Result getEvaluationCategories() {
        List<EvaluationCategories> categories = categoriesService.getEvaluationCategories();
        return Result.ok()
                .data("categories", categories); // 将列表存入data字段
    }

    @PostMapping
    public Result addEvaluationCategory(
            @Valid @RequestBody EvaluationCategoryRequest request) {
        try {
            categoriesService.addEvaluationCategory(request);
            return Result.ok()
                    .message("评分大类添加成功");
        } catch (RuntimeException e) {
            int code = e.getMessage().contains("已存在") ? 409 : 500;
            return Result.error()
                    .code(code)
                    .message(e.getMessage());
        }
    }


    @PutMapping("/{categoryId}")
    public Result updateEvaluationCategory(
            @PathVariable Long categoryId,
            @Valid @RequestBody EvaluationCategoryUpdateRequest request) {
        try {
            categoriesService.updateEvaluationCategory(categoryId, request);
            return Result.ok()
                    .message("评分大类信息更新成功");
        } catch (RuntimeException e) {
            int code;
            switch (e.getMessage()) {
                case "分类不存在":
                    code = 404;
                    break;
                case "分类名称已存在":
                    code = 409;
                    break;
                case "权重总和不能超过1":
                    code = 400;
                    break;
                default:
                    code = 500;
                    break;
            }
            return Result.error()
                    .code(code)
                    .message(e.getMessage());
        }
    }


    @DeleteMapping("/{categoryId}")
    public Result deleteEvaluationCategory(
            @PathVariable Long categoryId,
            @RequestHeader("Authorization") String token) {
        try {
            // 权限验证保持不变...
            String userId = (String) JwtUtil.getTokenBody(token).get("userId");
            Users user = usersService.getUserById(Long.valueOf(userId));
            if (!"admin".equals(user.getRole())) {
                return Result.error().code(403).message("无权限操作");
            }

            // 执行删除
            categoriesService.deleteEvaluationCategory(categoryId);
            return Result.ok().message("评分大类删除成功");

        } catch (RuntimeException e) {
            int code = e.getMessage().contains("不存在") ? 404 : 500;
            return Result.error()
                    .code(code)
                    .message(e.getMessage());
        }
    }
}

